package com.lanhe.teacher.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.teacher.domain.LhGroup;
import com.lanhe.teacher.domain.vo.StatTeacherByGroupVo;
import com.lanhe.teacher.mapper.LhGroupMapper;
import com.lanhe.teacher.service.ILhGroupService;
import com.lanhe.teacher.service.ILhTeacherService;

/**
 * 组表(LhGroup)表服务实现类
 *
 * @author zhanggl
 * @since 2023-09-14 22:28:46
 */
@Service
public class LhGroupServiceImpl implements ILhGroupService {

	@Autowired
	private LhGroupMapper groupMapper;

	/**
	 * 服务对象
	 */
	@Resource
	private ILhTeacherService lhTeacherService;

	/**
	 * 根据条件分页查询小组列表
	 *
	 * @param group 用户信息
	 * @return 小组信息集合信息
	 */
	@Override
	public List<LhGroup> selectGroupList(LhGroup group) {

		List<LhGroup> lhGroups = groupMapper.selectGroupList(group);
		List<Long> groupIds = lhGroups.stream().map(LhGroup::getGroupId).collect(Collectors.toList());

		Map<Long, Integer> groupMap = lhTeacherService.statTeacherByGroupIds(groupIds)
				.stream()
				.collect(Collectors.toMap(StatTeacherByGroupVo::getGroupId, StatTeacherByGroupVo::getNumber));

		lhGroups.forEach(lhGroup -> lhGroup.setTeacherNum(groupMap.getOrDefault(lhGroup.getGroupId(), 0)));

		return lhGroups;

	}

	/**
	 * 查询组信息
	 *
	 * @param groupId 组ID
	 * @return 组信息
	 */
	@Override
	public LhGroup selectGroupById(Long groupId) {
		return groupMapper.selectGroupById(groupId);
	}

	/**
	 * 查询组名称是否存在
	 *
	 * @param group 用户信息
	 * @return 结果
	 */
	@Override
	public boolean checkGroupNameUnique(LhGroup group) {
		LhGroup info = groupMapper.checkGroupNameUnique(group.getGroupName(),group.getGroupId());
		if (StringUtils.isNotNull(info))
		{
			return Constants.NOT_UNIQUE;
		}
		return Constants.UNIQUE;
	}

	/**
	 * 新增小组信息
	 *
	 * @param group 小组信息
	 * @return 结果
	 */
	@Override
	public int insertGroup(LhGroup group) {
		return groupMapper.insertGroup(group);
	}

	/**
	 * 修改组信息
	 *
	 * @param group 参数组信息
	 * @return 结果
	 */
	@Override
	public int updateGroup(LhGroup group) {
		return groupMapper.updateGroup(group);
	}

	/**
	 * 批量删除组信息
	 *
	 * @param groupIds 需要删除的参数ID
	 * @return 结果
	 */
	@Override
	public int deleteGroupByIds(Long[] groupIds) {
		return groupMapper.deleteGroupByIds(groupIds);
	}

	/**
	 * 批量查询组信息
	 *
	 * @param groupIds 需要删除的参数ID
	 * @return 结果
	 */
	@Override
	public List<LhGroup> selectGroupByIds(Long[] groupIds) {
		return groupMapper.selectGroupByIds(groupIds);
	}

	/**
	 * 统计老师数量增加
	 *
	 * @param groupId 参数组信息
	 * @return 结果
	 */
	@Override
	public int updateGroupTeacherNumInc(Long groupId) {
		return groupMapper.updateGroupTeacherNumInc(groupId);
	}

	/**
	 * 统计老师数量减少
	 *
	 * @param groupId 参数组信息
	 * @return 结果
	 */
	@Override
	public int updateGroupTeacherNumDec(Long groupId) {
		return groupMapper.updateGroupTeacherNumDec(groupId);
	}
}

